var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));
var Tx = require('ethereumjs-tx').Transaction;

//黑窗口第一个地址
var fromAddr = "0xf8c7CDa16522516aeB8BAd0E3B49412221Ce9D3c";
//第一个地址的私钥
var privateKey = Buffer.from('8d37ddfc871092c338ae2c4ad2b67ecf3b0ae5bd8eec004b228ad499324cb8fa', 'hex')
//合约地址
var contractAddr = '0x0E9ca048032ef8EAdDD1eC9Dc244F67d42F77207';
//wzabi文件
var abi = [{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_receivers","type":"address[]"},{"name":"_value","type":"uint256"}],"name":"batchTransfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"frozenAccount","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"target","type":"address"},{"name":"freeze","type":"bool"}],"name":"freezeAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"target","type":"address"},{"indexed":false,"name":"frozen","type":"bool"}],"name":"FrozenFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];

//构造函数:
var wzContract = new web3.eth.Contract(abi,contractAddr);

/* getBalance("0xfEEE7247C76dc5D0DaF7273CD4FE747e0854232C")
	function getBalance(addr){
		wzContract.methods.balanceOf(addr).call((err,result)=>{
			if(!err){
				console.log(addr+":"+web3.utils.fromWei(result,'mwei')+"USDT")
			}else{
				console.log("获取余额失败"+err)
			}
		});
	} */


//sendSignedTransaction2('0xD2AD33F6BCE60CB0206faE6Ebd9d722B440aaCA1','1');

function sendSignedTransaction2(toAddr,amount){
	var txData = wzContract.methods.transfer(toAddr,web3.utils.toWei(amount,'mwei')).encodeABI();
   web3.eth.getTransactionCount(fromAddr, 'pending',(err, nonce)=>{
	if(err){
		console.log(err)
	}
	 var rawTx = {
	   nonce: web3.utils.toHex(nonce),
	   gasPrice: web3.utils.toHex(web3.utils.toWei('20','gwei')),
	   gasLimit: web3.utils.toHex(3000000),
	   data: txData,
	   to: contractAddr
	 }
	 var tx = new Tx(rawTx);
	 tx.sign(privateKey);
	 	
	 var serializedTx = tx.serialize();
	
	web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),(error,hash)=>{
		if(!error){
			console.log(hash)
		}else{
			console.log(error)
		}
	})
})
}
////执行完 拿到交易凭证:  0x54f874b43780ee59584ae5014bc8067c374a26ae53189642b0e4224132bfd12e



//getTransactionReceipt('0x54f874b43780ee59584ae5014bc8067c374a26ae53189642b0e4224132bfd12e');

function getTransactionReceipt(Transaction){
	//返回指定交易的收据，使用哈希指定交易。需要指出的是，挂起的交易其收据无效。
	var receipt = web3.eth.getTransactionReceipt(Transaction)
	.then(console.log);
}






